﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ELab.QuestionAnwer;
using System.IO;
using ELab.Message;
using ELab.Client;
using ELab.Serialization.Data;
using ELab.Communication.Messages;
using ELab.Communication.Messages.ControllerMessages;

namespace ELab.Teacher.Controls
{
    public partial class ctrlMakeTestLesson : UserControl
    {
        private List<Question> PredefineQuestions = new List<Question>();
        private List<Question> SelectedQuestions = new List<Question>();
        public ELabClient TeacherControl { get; set; }
        public IEnumerable<User> Users { get; set; }
        public ctrlMakeTestLesson()
        {
            InitializeComponent();

            DataGridViewCheckBoxColumn checkColumn = new DataGridViewCheckBoxColumn();
            checkColumn.Name = "SelectedBox";
            checkColumn.HeaderText = "";
            checkColumn.Width = 30;
            checkColumn.ReadOnly = false;
            checkColumn.FillWeight = 10; //if the datagridview is resized (on form resize) the checkbox won't take up too much; value is relative to the other columns' fill values
            dataGridViewQuestions.Columns.Add(checkColumn);


            DataGridViewCheckBoxColumn checkColumn1 = new DataGridViewCheckBoxColumn();
            checkColumn1.Name = "SelectedBox";
            checkColumn1.HeaderText = "";
            checkColumn1.Width = 30;
            checkColumn1.ReadOnly = false;
            checkColumn1.FillWeight = 10; //if the datagridview is resized (on form resize) the checkbox won't take up too much; value is relative to the other columns' fill values
            dataGridViewSelectedQuestions.Columns.Add(checkColumn1);
        }
        private void ctrlMakeTestLesson_Load(object sender, EventArgs e)
        {
            LoadQuestions();
            BindQuestionGrid();
            dataGridViewSelectedQuestions.Rows.Clear();
            dataGridViewSelectedQuestions.Refresh();
        }
        private void LoadQuestions()
        {
            PredefineQuestions.Clear();
            SelectedQuestions.Clear();

            lbMessage.Text = "";
            List<Question> allQuestions = new List<Question>();
            //Load questions from server
            var response = (GetQuestionListResponseMessage)TeacherControl.Controller.SendMessageAndGetResponse(new GetQuestionListMessage());
            if (response.Questions != null)
            {
                allQuestions = response.Questions.ToList();
            }
            else lbMessage.Text = "Have not any question from response";

            PredefineQuestions = allQuestions;
        }

        private void btnStartTest_Click(object sender, EventArgs e)
        {
            var examp = new LessonTest();
            examp.TimeToTest = int.Parse(txtMinute.Text) * 60 + int.Parse(txtSeconds.Text);
            examp.Questions = SelectedQuestions;
     
            var dataArr = examp.SerializeToByteArray();

            Message.CommandType cmdType = Message.CommandType.RequestTestExample;
            if (cbEnableRaiseHandMode.Checked) cmdType = Message.CommandType.RequestTestExample_HandUp;
            CommandMessage comm = new CommandMessage { CommandType = cmdType, Body = dataArr };
            EMessage mess = new EMessage { Command = comm };

            //Cai nay nen truyen tu form Main vao
            if (TeacherControl != null)
            {
                foreach (User user in this.Users)
                {
                    var ms = TeacherControl.CreateMessage();
                    ms.DestinationApplicationName = user.UserName;
                    ms.MessageData = mess.Data;
                    ms.TransmitRule = MessageTransmitRules.NonPersistent;
                    ms.Send();
                }
            }

            lbMessage.Text = "The test lesson has been sent to the student for testing.";
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            ctrlQAManagement parent = (ctrlQAManagement)this.Parent.Parent;
            parent.RaiseCancel();
        }

        private void txtSearchQuestion_TextChanged(object sender, EventArgs e)
        {
            BindQuestionGrid(txtSearchQuestion.Text);
        }

        private void BindQuestionGrid(string filter = null)
        {
            dataGridViewQuestions.DataSource = PredefineQuestions.Where(x=>string.IsNullOrEmpty(filter) || x.QuestionValue.Contains(filter)).ToList();
            dataGridViewQuestions.Refresh();
        }

        private void btnAddSelectedQuestions_Click(object sender, EventArgs e)
        {
            var selectedQuestions = new List<Question>();
            foreach (DataGridViewRow dataGridRow in dataGridViewQuestions.Rows)
            {
                if (dataGridRow.Cells["SelectedBox"].Value != null && (bool)dataGridRow.Cells["SelectedBox"].Value)
                {
                    string question = (string)dataGridRow.Cells["QuestionValue"].Value;
                    selectedQuestions.Add(PredefineQuestions.Find(x => x.QuestionValue == question));
                }
            }

            SelectedQuestions.AddRange(selectedQuestions);
            foreach (var q in selectedQuestions)
            {
                PredefineQuestions.Remove(q);
            }

            dataGridViewQuestions.DataSource = new List<Question>();
            dataGridViewQuestions.DataSource = PredefineQuestions;
            dataGridViewSelectedQuestions.DataSource = new List<Question>();
            dataGridViewSelectedQuestions.DataSource = SelectedQuestions;
        }

        private void btnRemoveSelectedQuestions_Click(object sender, EventArgs e)
        {
            var selectedQuestions = new List<Question>();
            foreach (DataGridViewRow dataGridRow in dataGridViewSelectedQuestions.Rows)
            {
                if (dataGridRow.Cells["SelectedBox"].Value != null && (bool)dataGridRow.Cells["SelectedBox"].Value)
                {
                    string question = (string)dataGridRow.Cells[1].Value;
                    selectedQuestions.Add(SelectedQuestions.Find(x => x.QuestionValue == question));
                }
            }

           PredefineQuestions.AddRange(selectedQuestions);
            foreach (var q in selectedQuestions)
            {
                SelectedQuestions.Remove(q);
            }

            dataGridViewQuestions.DataSource = new List<Question>();
            dataGridViewQuestions.DataSource = PredefineQuestions;
            dataGridViewSelectedQuestions.DataSource = new List<Question>();
            dataGridViewSelectedQuestions.DataSource = SelectedQuestions;
        }
    }
}
